package com.yupi.oj.judge.codesandbox.impl;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.yupi.oj.common.ErrorCode;
import com.yupi.oj.exception.BusinessException;
import com.yupi.oj.judge.codesandbox.CodeSandBox;
import com.yupi.oj.judge.codesandbox.model.ExecuteCodeRequest;
import com.yupi.oj.judge.codesandbox.model.ExecuteCodeResponse;
import java.util.Arrays;
public class RemoteCodeSandBox implements CodeSandBox {
    //请求头添加的秘钥
    private static final String REQUEST_HEADER = "key";
    private static final String REQUEST_SECRET="secretValue";
    @Override
    public ExecuteCodeResponse codeExecute(ExecuteCodeRequest executeCodeRequest) {
        //向代码沙箱服务器发送请求
        String url = "http://122.51.81.186:8081/codeExecute";
//        String url = "http://192.168.190.128:8081/codeExecute";
        HttpRequest httpRequest = HttpUtil.createPost(url);
        String body = httpRequest
                .header(REQUEST_HEADER, Arrays.toString(DigestUtil.md5(REQUEST_SECRET)))
                .body(JSONUtil.toJsonStr(executeCodeRequest))
                .execute()
                .body();
        if(body==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"调用接口失败");
        }
        return JSONUtil.toBean(body, ExecuteCodeResponse.class);
    }
}
